/*
 * @Description  : 用户信息状态管理
 * @Author       : YiDing Xue
 * @Date         : 2023-01-12
 * @LastEditors  : YiDing Xue
 * @LastEditTime : 2023-03-11
 */
import { defineStore } from 'pinia'
import { resetRouter } from '@/router'
import { removeToken } from '@/utils'
import { userInfo } from '~/src/api/auth'

export const useVisaStore = defineStore('visa', {
  state() {
    return {
      userInfo: {},
      permanentAddress: '',
      countryList: [],
      addressList: [],
      provinceList: [],
      consulateCityList: [],
      countyList: [],
      visaOrderId: '',
      needList: [],
      uploadInfo:
        (window.sessionStorage.getItem('uploadInfo') && JSON.parse(window.sessionStorage.getItem('uploadInfo'))) || {},
      identityMaterial: {},
      alreadyUploadMaterial: [],
      sign: window.sessionStorage.getItem('sign')
    }
  },
  // 数据持久化
  persist: true,
  getters: {
    userId() {
      return this.userInfo?.id
    },
    name() {
      return this.userInfo?.realName
    },
    phoneNumber() {
      return this.userInfo?.phoneNumber
    },
    avatar() {
      return (
        this.userInfo?.avatar ||
        'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202003%2F31%2F20200331000349_odinr.thumb.1000_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1676445368&t=17ab7a824b08d86bd59122c238fd5aad'
      )
    }
  },
  actions: {
    async getUserInfo() {
      try {
        const result = await userInfo()
        this.userInfo = result.data
        return Promise.resolve(result.data)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    async logout() {
      removeToken()
      resetRouter()
      this.$reset()
    },
    setUserInfo(userInfo = {}) {
      this.userInfo = { ...this.userInfo, ...userInfo }
    },
    setPermanentAddress(value) {
      this.permanentAddress = value
    },
    setVisaOrderId(value) {
      this.visaOrderId = value
    },
    setNeedList(value) {
      this.needList = value
    },
    setUploadInfo(value) {
      this.uploadInfo = value
      window.sessionStorage.setItem('uploadInfo', JSON.stringify(value))
    },
    setIdentityMaterial(value) {
      this.identityMaterial = value
    },
    setAlreadyUploadMaterial(value) {
      this.alreadyUploadMaterial = value
    },
    setSign(value) {
      this.sign = value
      window.sessionStorage.setItem('sign', value)
    },
    setCountryList(value) {
      this.countryList = value
    },
    setAddressList(value) {
      console.log('%c [ value ]-69', 'font-size:13px; background:#741a19; color:#b85e5d;', value)
      this.addressList = value
    },
    setProvinceList(value) {
      this.provinceList = value
    },
    setCityList(value) {
      this.consulateCityList = value
      window.localStorage.setItem('consulateCityList', JSON.stringify(value))
    },
    setCountyList(value) {
      this.countyList = value
    }
  }
})
